#include "ManageTool.h"
#include "LogComm.h"
#include "globe.h"
#include "servant/Application.h"
#include "SocialServant.h"
#include "ServiceDefine.h"
#include "SocialServer.h"
#include "TimeUtil.h"
#include "global.pb.h"

time_t ManageToolStrTime2Unix(string timeStamp)
{
    struct tm tm;
    memset(&tm, 0, sizeof(tm));
    sscanf(timeStamp.c_str(), "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday, &tm.tm_hour, &tm.tm_min, &tm.tm_sec);
    tm.tm_year -= 1900;
    tm.tm_mon--;
    return mktime(&tm);
}

// 生产消息
void ManageToolGenMessge(int iType, long uId, long lBalance, long cId, string sClubName, long pId)
{
    LOG_DEBUG << "ManageToolGenMessge:" << iType << ":" << uId << ":" << lBalance << ":" << cId << ":" << sClubName << ":" << pId << endl;

    global::MessageReq req;
    req.iMailType = global::E_MESSAGE_CLUB_GOLD;
    req.lPlayerID = uId;
    req.lAmount = lBalance;
    req.iContentType = iType;
    req.lClubID = cId;
    req.sClubName = sClubName;
    if (iType != GlobalProto::E_MESSAGE_TYPE::E_MESSAGE_WARN)
    {
        req.sPresidentID = pId;
    }
    else
    {
        // 预警信息 uid就是会长ID pId当补充的金额度用
        req.iMailType = global::E_MESSAGE_WARN;
        req.iContentType = 1;
        req.sPresidentID = uId;
        req.sContent = L2S(pId);
    }
    g_app.getOuterFactoryPtr()->asyncClubGenMessage(req);
}

// 财富纯日志
void ManageToolLogCoinChange(long uId, long lChangeCoin, long lBalance, int iType, long lRelateId)
{
    LOG_DEBUG << "ManageToolLogCoinChange:" << uId << ":" << lChangeCoin << ":" << lBalance << ":" << iType << "?" << lRelateId << endl;

    DaqiGame::TLog2DBReq tLog2DBReq;
    tLog2DBReq.sLogType = 28;

    vector<string> veclog;
    veclog.push_back(L2S(uId));
    veclog.push_back(I2S(1000));
    veclog.push_back(I2S(iType));
    veclog.push_back(L2S(lChangeCoin));
    veclog.push_back(L2S(lBalance));
    veclog.push_back(L2S(lRelateId));
    veclog.push_back("0");
    
    tLog2DBReq.vecLogData.push_back(veclog);

    g_app.getOuterFactoryPtr()->asyncLog2DB(uId, tLog2DBReq);
}

// 财富更改
void ManageToolChangeUWealth(long uId, long iChangeGoldCoin, int iType)
{
    LOG_DEBUG << "ManageToolChangeUWealth:" << uId << ":" << iChangeGoldCoin << ":" << iType << endl;

    userinfo::ModifyUserWealthReq req;
    req.uid = uId;
    req.changeType = iType;
    req.goldChange = iChangeGoldCoin;
    req.relateId = "club";
    g_app.getOuterFactoryPtr()->async2ModifyUWealth(req);
}

// 财富更新(tg账号)
void ManageToolModifyWalletBalance(long uId, long iChangeGoldCoin, int iType)
{
    LOG_DEBUG << "ManageToolModifyBalance:" << uId << ":" << iChangeGoldCoin << ":" << iType << endl;

    order::ModifyWalletBalanceReq req;
    req.uid = uId;
    req.type = iType;
    req.amount = iChangeGoldCoin;
    req.roomkey = "relateId";
    g_app.getOuterFactoryPtr()->async2ModifyUWalletBalance(req);
}

// 查询财富信息(tg账号)
long ManageToolGetWalletBalance(long uId)
{
    long  balance = 0;
    balance = g_app.getOuterFactoryPtr()->selectWalletBalance(uId);

    LOG_DEBUG << "ManageToolGetWalletBalance balance:" << balance << endl;
    return balance;
}

// 同步扣钱
bool ManageToolTakeUWealth(long uId, int iFlag, long lAmount, int iType, string sRelateId)
{
    LOG_DEBUG << "ManageToolTakeUWealth:" << uId << ":" << iFlag << ":" << lAmount << ":" << iType << "?" << sRelateId << endl;

    userinfo::ModifyUserWealthReq req;
    req.uid = uId;
    req.changeType = iType;
    if (iFlag == 0)
    {
        req.diamondChange = lAmount;
    }
    else
    {
        req.goldChange = lAmount;
    }
    req.relateId = sRelateId;
    userinfo::ModifyUserWealthResp resp;
    int iRet = g_app.getOuterFactoryPtr()->getHallServantPrx(uId)->modifyUserWealth(req, resp);
    if (iRet != 0)
    {
        LOG_ERROR << "modifyUserWealth err. iRet: " << iRet << endl;
        return false;
    }

    return true;
}

// 获取俱乐部最近更新时间戳
tars::Int32 ManageToolGetActTime(const DBAgentServantPrx prx, tars::Int64 id, int &dayTime, int &monthTime)
{
    FUNC_ENTRY("");
    int iRet = 0;
     __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageToolGetActTime prx null!" << endl;
        return -1;
    }

    dataproxy::TReadDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_LIST) + ":" + I2S(CLUB_ACT_TIME_UID) + ":" + L2S(id);
    dataReq.operateType = E_REDIS_READ;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_STRING;
    dataReq.clusterInfo.frageFactor = tars::hash<string>()(L2S(id));;

    TReadDataRsp dataRsp;
    iRet = prx->redisRead(dataReq, dataRsp);
    if(iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_DEBUG << "ManageToolGetActTime failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << endl;
        return -1;
    }

    ROLLLOG_DEBUG << "ManageToolGetActTime ok! dataRsp:" << printTars(dataRsp) << endl;

    for (auto it = dataRsp.fields.begin(); it != dataRsp.fields.end(); ++it)
    {
        for (auto itfield = it->begin(); itfield != it->end(); ++itfield)
        {
            if (itfield->colName == "day_time")
            {
                dayTime = S2L(itfield->colValue);
            }
            else if (itfield->colName == "month_time")
            {
                monthTime = S2L(itfield->colValue);
            }
        }
        break;
    }

    return iRet;

    __CATCH__
    FUNC_EXIT("", iRet);

    return iRet;
}

// 更新俱乐部最近时间戳
tars::Int32 ManageToolModifyActTime(const DBAgentServantPrx prx, tars::Int64 id, int flag, int actTime)
{
    FUNC_ENTRY("");
    int iRet = 0;
    __TRY__

    if (!prx)
    {
        ROLLLOG_ERROR << "ManageToolModifyActTime prx null!" << endl;
        return -1;
    }

    dataproxy::TWriteDataReq dataReq;
    dataReq.resetDefautlt();
    dataReq.keyName = I2S(E_REDIS_TYPE_HASH) + ":" + I2S(CLUB_ACT_TIME_UID) + ":" + L2S(id);
    dataReq.operateType = E_REDIS_WRITE;
    dataReq.paraExt.resetDefautlt();
    dataReq.paraExt.queryType = dbagent::E_UPDATE;
    dataReq.clusterInfo.resetDefautlt();
    dataReq.clusterInfo.busiType = E_REDIS_PROPERTY;
    dataReq.clusterInfo.frageFactorType = E_FRAGE_FACTOR_USER_ID;
    dataReq.clusterInfo.frageFactor = id;

    vector<TField> fields;
    TField tfield;
    tfield.colArithType = E_NONE;
    if (flag == 0)
    {
        tfield.colName = "day_time";
    }
    else
    {
        tfield.colName = "month_time";
    }
    tfield.colType = INT;
    tfield.colValue = I2S(actTime);
    fields.push_back(tfield);

    dataReq.fields = fields;

    dataproxy::TWriteDataRsp dataRsp;
    iRet = prx->redisWrite(dataReq, dataRsp);
    if (iRet != 0 || dataRsp.iResult != 0)
    {
        ROLLLOG_ERROR << "ManageToolModifyActTime failed! iRet:" << iRet << ", iResult:" << dataRsp.iResult << ", dataReq:" << printTars(dataReq) << endl;
        return -2;
    }

    ROLLLOG_DEBUG << "ManageToolModifyActTime ok! dataReq:" << printTars(dataReq) << ", dataRsp: " << printTars(dataRsp) << endl;

    __CATCH__
    FUNC_EXIT("", iRet);
    return iRet;
}